package org.example.edusmart.utils;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson2.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.HashMap;

@Component
public class Authorization {

    @Value("${authorization.secretKey}")
    private String secretKey; // 导入秘钥

    @Value("${authorization.studentUrl}")
    private String studentUrl; //学生接口路径

    @Value("${authorization.teacherUrl}")
    private String teacherUrl; //教师接口路径

    /**
     * 校验学生接口
     *
     * @param params 前端传的用户名和学号
     * @return
     */
    public HashMap<String, Object> getStudentAuthorization(HashMap<String, Object> params) {
        // 将秘钥添加到请求体中
        params.put("secretKey", secretKey);


        // 获得http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Post请求
        HttpPost httpPost = new HttpPost(studentUrl);

        //添加参数到请求中
        StringEntity entity = new StringEntity(JSONObject.toJSONString(params), "utf-8");
        httpPost.setEntity(entity);

        // 设置请求头，请求参数为JSON格式
        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
        try {
            // 执行Post请求
            var response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String responseJson = EntityUtils.toString(responseEntity);
                return JSON.parseObject(responseJson, HashMap.class);
            }
        } catch (Exception e) {
            //请求失败
            return new HashMap<>() {{
                put("code", 300);
                put("message", e.getMessage());
            }};
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                //关闭失败，不做处理
            }
        }
        return null;
    }

    /**
     * 检验老师接口
     * @param params 前端传的用户名和学号
     * @return
     */
    public HashMap<String, Object> getTeacherAuthorization(HashMap<String, Object> params) {
        // 将秘钥添加到请求体中
        params.put("secretKey", secretKey);

        // 获得http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        // 创建Post请求
        HttpPost httpPost = new HttpPost(teacherUrl);

        //添加参数到请求中
        StringEntity entity = new StringEntity(JSONObject.toJSONString(params), "utf-8");
        httpPost.setEntity(entity);

        // 设置请求头，请求参数为JSON格式
        httpPost.setHeader("Content-Type", "application/json;charset=utf-8");
        try {
            // 执行Post请求
            var response = httpClient.execute(httpPost);
            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                String responseJson = EntityUtils.toString(responseEntity);
                return JSON.parseObject(responseJson, HashMap.class);
            }
        } catch (Exception e) {
            //请求失败
            return new HashMap<>() {{
                put("code", 300);
                put("message", e.getMessage());
            }};
        } finally {
            try {
                httpClient.close();
            } catch (IOException e) {
                //关闭失败，不做处理
            }
        }
        return null;
    }
}
